﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
   

    public class EventClass
    {

        public void Show()
        {
            var publisher = new Publisher();
            var subscriber = new Subscriber();
            var subscriber2 = new Subscriber();
            var mao = new Mao();
            
            publisher.showEvent += new Publisher.ShowEvent(subscriber.Handler);
            publisher.showEvent += new Publisher.ShowEvent(subscriber2.Handler);
            publisher.showEvent += new Publisher.ShowEvent(mao.Handler);

            var result = publisher.Begin(); //这个能获取最后一个事件得到的结果

        }
    }


    /// <summary>
    /// 发布者
    /// </summary>
    public class Publisher
    {
        public delegate int ShowEvent(int i);
        public event ShowEvent showEvent;//发布事件

        public int Begin()
        {
           var result= showEvent(3);//触发事件
            return result;
        }
    }

    /// <summary>
    /// 订阅者
    /// </summary>
    public class Subscriber
    {
        public int Handler(int i)
        {
            Console.WriteLine($"Subscriber:i*i={i*i}");
            return i*i;
        }
    }

    public class Mao
    {
        public int  Handler(int i)
        {
            Console.WriteLine($"Mao:i+i={i + i}");
            return i+i;
        }
    }
}
